package com.xiaowu.netmusic.controller;

import com.xiaowu.netmusic.domain.dto.PlaylistCreateDTO;
import com.xiaowu.netmusic.domain.vo.PlaylistDetailVO;
import com.xiaowu.netmusic.domain.vo.PlaylistVO;
import com.xiaowu.netmusic.service.PlaylistService;
import com.xiaowu.netmusic.service.PlaylistSongService;
import com.xiaowu.netmusic.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/playlist")
@RequiredArgsConstructor
@Tag(name = "歌单管理", description = "歌单的增删查改 API")
public class PlaylistController {

    private final PlaylistService playlistService;
    private final PlaylistSongService playlistSongService;

    @PostMapping
    @Operation(summary = "创建歌单", description = "创建一个新的歌单")
    public Result<Void> createPlaylist(@Valid @RequestBody PlaylistCreateDTO dto, @RequestParam Long userId) {
        try {
            playlistService.createPlaylist(dto, userId);
            return Result.success();
        } catch (Exception e) {
            return Result.error("创建歌单失败: " + e.getMessage());
        }
    }

    @GetMapping("/user/{userId}")
    @Operation(summary = "查看我的歌单", description = "获取指定用户的所有歌单")
    public Result<List<PlaylistVO>> getUserPlaylists(@PathVariable Long userId) {
        try {
            List<PlaylistVO> playlists = playlistService.getPlaylistsByUser(userId);
            return Result.success(playlists);
        } catch (Exception e) {
            return Result.error("获取歌单失败: " + e.getMessage());
        }
    }

    @GetMapping("/{playlistId}")
    @Operation(summary = "查看歌单详情", description = "查看某个歌单的详细信息和歌曲列表")
    public Result<PlaylistDetailVO> getPlaylistDetail(@PathVariable Long playlistId) {
        try {
            PlaylistDetailVO playlistDetailVO = playlistService.getPlaylistDetail(playlistId);
            return Result.success(playlistDetailVO);
        } catch (Exception e) {
            return Result.error("获取歌单详情失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/{playlistId}")
    @Operation(summary = "删除歌单", description = "根据歌单ID删除歌单")
    public Result<Void> deletePlaylist(@PathVariable Long playlistId) {
        try {
            playlistService.deletePlaylist(playlistId);
            return Result.success();
        } catch (Exception e) {
            return Result.error("删除歌单失败: " + e.getMessage());
        }
    }

    @PostMapping("/{playlistId}/add-song/{songId}")
    @Operation(summary = "添加歌曲到歌单", description = "将歌曲添加到指定歌单中")
    public Result<Void> addSongToPlaylist(@PathVariable Long playlistId, @PathVariable Long songId) {
        try {
            playlistSongService.addSongToPlaylist(playlistId, songId);
            return Result.success();
        } catch (Exception e) {
            return Result.error("添加歌曲失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/{playlistId}/remove-song/{songId}")
    @Operation(summary = "从歌单中移除歌曲", description = "将歌曲从指定歌单中移除")
    public Result<Void> removeSongFromPlaylist(@PathVariable Long playlistId, @PathVariable Long songId) {
        try {
            playlistSongService.removeSongFromPlaylist(playlistId, songId);
            return Result.success();
        } catch (Exception e) {
            return Result.error("移除歌曲失败: " + e.getMessage());
        }
    }
}
